package org.csu.mypetstore.controller;

import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/catalog")
@SessionAttributes("allCategoryList")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

//    @GetMapping ("/main")
//    public String loginForm(Model model){
//        List<Category> allCategoryList = catalogService.getAllCategoryList();
//        model.addAttribute("allCategoryList",allCategoryList);
//
//        return "/catalog/Main";
//    }
    @GetMapping ("/main")
    public CommonResponse<Object> loginForm(){
        List<Category> allCategoryList = catalogService.getAllCategoryList();
        return  CommonResponse.createForSuccess(allCategoryList);
    }

//    @GetMapping ("/viewCategory")
//    public String viewCatalog(Category category, Model model){
//        String categoryId=category.getCategoryId();
//        Category categoryByCategoryId=catalogService.getCategory(categoryId);
//        List<Product> productList=catalogService.getProductListByCategory(categoryId);
//
//        model.addAttribute("productList",productList);
//        model.addAttribute("category",categoryByCategoryId);
//        return "catalog/Category";
//    }
    @PostMapping ("/viewCategory")
     @ResponseBody
    public CommonResponse<Object> viewCatalog(@RequestParam("categoryId") String categoryId){
         System.out.println("categoryId: " + categoryId);
        Category categoryByCategoryId=catalogService.getCategory(categoryId);
        List<Product> productList=catalogService.getProductListByCategory(categoryId);

        return CommonResponse.createForSuccess(productList,categoryByCategoryId);
    }


//    @GetMapping("/viewProduct")
//    public String viewProduct(Product product, Model model){
//        List<Item> itemList=catalogService.getItemListByProduct(product.getProductId());
//        model.addAttribute("product",product);
//        model.addAttribute("itemList",itemList);
//        return "/catalog/Product";
//    }
    @PostMapping("/viewProduct")
    public CommonResponse<Object> viewProduct(@RequestParam("productId") String productId){
        List<Item> itemList=catalogService.getItemListByProduct(productId);
        Product product=catalogService.getProduct(productId);
        return CommonResponse.createForSuccess(product,itemList);
    }

//    @GetMapping("/viewItem")
//    public String viewItem(Item item_post,Product product,Model model){
//        Item item=catalogService.getItem(item_post.getItemId());
//        //库存量
//        int item1=catalogService.getInventoryQuantity(item.getItemId());
//
//        model.addAttribute("item",item);
//        model.addAttribute("item1",item1);
//        model.addAttribute("product",product);
//        return "/catalog/Item";
//    }
    @PostMapping ("/viewItem")
    public CommonResponse<Object> viewItem(@RequestParam("itemId") String itemId){
        Item item=catalogService.getItem(itemId);
        //库存量
        //int item1=catalogService.getInventoryQuantity(itemId);

        return CommonResponse.createForSuccess(item);
    }

//    @PostMapping("/search")
//    public String searchProduct(String keyword,Model model){
//        List<Product> productList=new ArrayList<Product>();
//        productList=catalogService.searchProductList(keyword);
//
//        model.addAttribute("productList",productList);
//        return "/catalog/SearchProduct";
//    }
    @PostMapping("/search")
    public CommonResponse<Object> searchProduct(@RequestParam("keyword") String keyword){
        List<Product> productList=new ArrayList<Product>();
        productList=catalogService.searchProductList(keyword);
        return CommonResponse.createForSuccess(productList);
    }

//    @GetMapping("/searchThis")
//    public void searchAutoComplete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String keyword;
//        keyword=request.getParameter("keyword");
//        Product product=new Product();
//        Item item=new Item();
//        List<Product> productList=new ArrayList<Product>();
//        productList=catalogService.searchProductList(keyword);
//
//        StringBuffer sb = new StringBuffer("[");
//
//        for(int i=0;i<productList.size();i++){
//            if(i== productList.size()-1) {
//                sb.append("\"" + productList.get(i).getName() + "\"]");
//            }else{
//                sb.append("\"" + productList.get(i).getName() + "\",");
//            }
//        }
//        response.getWriter().write(sb.toString());
//    }

@PostMapping("/categoryShowJsServlet")
    protected CommonResponse<Object> showProduct(@RequestParam("categoryId") String categoryId) throws ServletException, IOException {
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        for(Product product:productList){
            System.out.println(product.getProductId()+product.getCategoryId()+product.getName());
        }
        String mouseevent = " ";
        mouseevent = categoryId+"\n"+"Product ID      Name\n";
        int i = 0;
        while(i < productList.size()){
            Product product = productList.get(i);
            mouseevent += product.getProductId() + "      " + product.getName() + "\n";
            i++;
        }
        return CommonResponse.createForSuccess(mouseevent);

    }



}
